//
//  tstShuffle.c
//  dominion
//
//  Created by Doug Mason on 3/8/14.
//  Copyright (c) 2014 Doug Mason. All rights reserved.
//

#include <stdio.h>
#include "tstShuffle.h"
/*
 deckCount is equal before and after the shuffle assuming deck count is greater or equal to one.
 If deckCount is less than 1, function should fail and return -1.
 Ensure that all cards by type and quantity remain in the deck before and after the function terminates.
 Dependancies: qsort() from stdlib.h , struct gameState implementation
*/


int tstShuffle(int player, struct gameState *state) {
    int numErrors=0,i;
    int preCount = state->deckCount[player];
    int old_deck[MAX_DECK];
    int new_deck[MAX_DECK];
    for (i=0; i<preCount; i++) {
        old_deck[i] = state->deck[player][i];
    }
    
    int shuffle_result = shuffle(player,state);
    
    if((preCount<=0) &&(shuffle_result!=-1)){
        printf("Failed to exit on null deck size!\n");
        numErrors+=1;
    }
    else{
        for (i=0; i<preCount; i++) {
            new_deck[i] = state->deck[player][i];
        }
        if (preCount !=state->deckCount[player]) {
            printf("Did not properly keep card count after shuffle!!! precount: %d current count %d\n",preCount,state->deckCount[player]);
            numErrors+=1;
        }
    }
    //Assuming we have the same deck size, we should have the exact same array given an equal array and applying a
    //sorting algorithm on them. We choose qsort, as Quicksort has a very good runtime for most arrays.
    qsort(((void*)old_deck),preCount,sizeof(int),compare);
    qsort(((void*)new_deck),state->deckCount[player],sizeof(int),compare);

    for(i = 0; i < preCount;i++){
        if (old_deck[i]!=new_deck[i]) {
            printf("We now have a different card as a result of shuffle! old card: %d \t new card: %d \n ",old_deck[i],new_deck[i]);
            numErrors+=1;
        }
    }
    
    if(numErrors == 0){
        printf("Shuffle occured without errors.\n");
        return 0;
    }
    else{
        printf("Number of errors that has occured during test: %d",numErrors);
        return -1;
    }
}